home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / include / bits / byteswap.h < prev    next >
C/C++ Source or Header  |  2008-09-29  |  5KB  |  137 lines

  1. /* Macros to swap the order of bytes in integer values.
  2.    Copyright (C) 1997, 1998, 2000, 2002, 2003, 2007
  3.    Free Software Foundation, Inc.
  4.    This file is part of the GNU C Library.
  5.  
  6.    The GNU C Library is free software; you can redistribute it and/or
  7.    modify it under the terms of the GNU Lesser General Public
  8.    License as published by the Free Software Foundation; either
  9.    version 2.1 of the License, or (at your option) any later version.
  10.  
  11.    The GNU C Library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    Lesser General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU Lesser General Public
  17.    License along with the GNU C Library; if not, write to the Free
  18.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19.    02111-1307 USA.  */
  20.  
  21. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
  22. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  23. #endif
  24.  
  25. #ifndef _BITS_BYTESWAP_H
  26. #define _BITS_BYTESWAP_H 1
  27.  
  28. #include <bits/wordsize.h>
  29.  
  30. /* Swap bytes in 16 bit value.  */
  31. #define __bswap_constant_16(x) \
  32.      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  33.  
  34. #if defined __GNUC__ && __GNUC__ >= 2
  35. # define __bswap_16(x) \
  36.      (__extension__                                  \
  37.       ({ register unsigned short int __v, __x = (x);                  \
  38.      if (__builtin_constant_p (__x))                      \
  39.        __v = __bswap_constant_16 (__x);                      \
  40.      else                                      \
  41.        __asm__ ("rorw $8, %w0"                          \
  42.             : "=r" (__v)                          \
  43.             : "0" (__x)                              \
  44.             : "cc");                              \
  45.      __v; }))
  46. #else
  47. /* This is better than nothing.  */
  48. # define __bswap_16(x) \
  49.      (__extension__                                  \
  50.       ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
  51. #endif
  52.  
  53.  
  54. /* Swap bytes in 32 bit value.  */
  55. #define __bswap_constant_32(x) \
  56.      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |              \
  57.       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
  58.  
  59. #if defined __GNUC__ && __GNUC__ >= 2
  60. # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__          \
  61.               || defined __pentiumpro__ || defined __pentium4__   \
  62.               || defined __k8__ || defined __athlon__          \
  63.               || defined __k6__ || defined __nocona__          \
  64.               || defined __core2__ || defined __geode__          \
  65.               || defined __amdfam10__)
  66. /* To swap the bytes in a word the i486 processors and up provide the
  67.    `bswap' opcode.  On i386 we have to use three instructions.  */
  68. #  define __bswap_32(x) \
  69.      (__extension__                                  \
  70.       ({ register unsigned int __v, __x = (x);                      \
  71.      if (__builtin_constant_p (__x))                      \
  72.        __v = __bswap_constant_32 (__x);                      \
  73.      else                                      \
  74.        __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));              \
  75.      __v; }))
  76. # else
  77. #  define __bswap_32(x)                                  \
  78.      (__extension__                                  \
  79.       ({ register unsigned int __v, __x = (x);                      \
  80.      if (__builtin_constant_p (__x))                      \
  81.        __v = __bswap_constant_32 (__x);                      \
  82.      else                                      \
  83.        __asm__ ("rorw $8, %w0;"                          \
  84.             "rorl $16, %0;"                          \
  85.             "rorw $8, %w0"                          \
  86.             : "=r" (__v)                          \
  87.             : "0" (__x)                              \
  88.             : "cc");                              \
  89.      __v; }))
  90. # endif
  91. #else
  92. # define __bswap_32(x) \
  93.      (__extension__                                  \
  94.       ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
  95. #endif
  96.  
  97.  
  98. #if defined __GNUC__ && __GNUC__ >= 2
  99. /* Swap bytes in 64 bit value.  */
  100. # define __bswap_constant_64(x) \
  101.      ((((x) & 0xff00000000000000ull) >> 56)                      \
  102.       | (((x) & 0x00ff000000000000ull) >> 40)                      \
  103.       | (((x) & 0x0000ff0000000000ull) >> 24)                      \
  104.       | (((x) & 0x000000ff00000000ull) >> 8)                      \
  105.       | (((x) & 0x00000000ff000000ull) << 8)                      \
  106.       | (((x) & 0x0000000000ff0000ull) << 24)                      \
  107.       | (((x) & 0x000000000000ff00ull) << 40)                      \
  108.       | (((x) & 0x00000000000000ffull) << 56))
  109.  
  110. # if __WORDSIZE == 64
  111. #  define __bswap_64(x) \
  112.      (__extension__                                  \
  113.       ({ register unsigned long __v, __x = (x);                      \
  114.      if (__builtin_constant_p (__x))                      \
  115.        __v = __bswap_constant_64 (__x);                      \
  116.      else                                      \
  117.        __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x));              \
  118.      __v; }))
  119. # else
  120. #  define __bswap_64(x) \
  121.      (__extension__                                                           \
  122.       ({ union { __extension__ unsigned long long int __ll;                   \
  123.                  unsigned int __l[2]; } __w, __r;                             \
  124.          if (__builtin_constant_p (x))                                        \
  125.            __r.__ll = __bswap_constant_64 (x);                                \
  126.          else                                                                 \
  127.            {                                                                  \
  128.              __w.__ll = (x);                                                  \
  129.              __r.__l[0] = __bswap_32 (__w.__l[1]);                            \
  130.              __r.__l[1] = __bswap_32 (__w.__l[0]);                            \
  131.            }                                                                  \
  132.          __r.__ll; }))
  133. # endif
  134. #endif
  135.  
  136. #endif /* _BITS_BYTESWAP_H */
  137.